//
//  revisePasWordController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/15.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "revisePasWordController.h"
#import <QMUIKit/QMUIKit.h>

@interface revisePasWordController ()<QMUITextFieldDelegate>
{
    
    UIButton *_sureBtn;//确定按钮
    UIButton *_cancelBtn;//取消按钮
}
/**
 *  装文本框的数组
 */
@property (nonatomic,strong)  NSMutableArray *textFiledArray;
@end

@implementation revisePasWordController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"修改交易密码";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setApperance];
    
}
- (void)setApperance{
    
    // 设置五个文本框
    // 占位文字的数组
    NSArray *placeHolders = @[@"请输入旧密码",@"请输入新密码",@"请再次输入密码"];

    // 设置一个指针，指向文本框
    UITextField *currentFiled = nil;
    //添加文本框
    for (int index = 0; index < placeHolders.count; index++) {
        
        QMUITextField *textField = [[QMUITextField alloc]init];
        [self.textFiledArray addObject:textField];
        textField.returnKeyType = UIReturnKeyDone;
        [textField setDelegate: self];
        textField.tag = 200 +index;
        textField.font = [SSSFont systemFontOfSize:14];
        [self.view addSubview:textField];
        textField.placeholder = placeHolders[index];
        textField.textColor = [UIColor grayColor];
        textField.layer.borderWidth = 0.5;
        textField.layer.borderColor = [[UIColor grayColor]colorWithAlphaComponent:0.3].CGColor;
        textField.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
        textField.layer.masksToBounds = YES;
        // 设置文本框的清楚按钮的模式
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        // 设置文本框的清楚按钮的模式
//        textField.clearsOnBeginEditing = YES;
        // 设置文本框的光标颜色
        textField.tintColor = [UIColor colorWithHex:@"44b034"];
        // [self setLfetView:imageNamesArray[index] isLeft:YES textField:textField];
        //[self setLfetImageView:imageNamesArray[index] isLeft:YES textField:textField];
        
        //布局约束
        //ZD_WEAKSELF;
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).offset(WidthIos6CompeteWithCurrent(43));
            
                
                make.right.equalTo(self.view).offset(WidthIos6CompeteWithCurrent(-43));
            
            make.height.equalTo(@(HeigthIos6CompeteWithCurrent(51)));
            
            if (currentFiled == nil) {
                
                make.top.equalTo(self.view).offset(HeigthIos6CompeteWithCurrent(36)+64);
            }
            else {
                make.top.equalTo(currentFiled.mas_bottom).offset(HeigthIos6CompeteWithCurrent(15));
            }
            
        }];
        currentFiled = textField;
        textField.secureTextEntry = YES;
    }
    
    
    //确定按钮
    _sureBtn = [[UIButton alloc]init];
    [_sureBtn setTitle:@"确 定" forState:UIControlStateNormal];
    _sureBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_sureBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
    _sureBtn.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    _sureBtn.layer.masksToBounds = YES;
    [_sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:_sureBtn];
    
    //添加布局约束
    _sureBtn.sd_layout
    .topSpaceToView(currentFiled,HeigthIos6CompeteWithCurrent(30))
    .leftEqualToView(currentFiled)
    .rightEqualToView(currentFiled)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    
    //取消按钮
    
    _cancelBtn = [[UIButton alloc]init];
    [_cancelBtn setTitle:@"取 消" forState:UIControlStateNormal];
    [_cancelBtn setTitleColor:[UIColor colorWithHex:@"46B77D"] forState:UIControlStateNormal];
    [_cancelBtn setBackgroundColor:[UIColor clearColor]];
    _cancelBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:_cancelBtn];
    _cancelBtn.sd_layout
    .topSpaceToView(_sureBtn,HeigthIos6CompeteWithCurrent(20))
    .leftEqualToView(_sureBtn)
    .rightEqualToView(_sureBtn)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    _cancelBtn.layer.cornerRadius = HeigthIos6CompeteWithCurrent(10);
    _cancelBtn.layer.masksToBounds = YES;
    _cancelBtn.layer.borderColor = [UIColor colorWithHex:@"46B77D"].CGColor;
    _cancelBtn.layer.borderWidth = 0.5;

}
#pragma mark - 确定按钮
- (void)sureBtnClick{
    
    // 让所有的文本框放弃第一响应者
    [self doResignFirstResponder];
    
   
    if ([[self.textFiledArray[0] text] isEqualToString:@""]){
        showTipHudStr(@"请输入旧密码");
        return;
    }
    if ([[self.textFiledArray[1] text] isEqualToString:@""]){
        showTipHudStr(@"请输入新密码");
        return;
    }
    if ([[self.textFiledArray[2] text] isEqualToString:@""]){
        showTipHudStr(@"请再次输入新密码");
        return;
    }
    if ([self.textFiledArray[1] text].length<6) {
        showTipHudStr(@"新密码不少于六位数");
        return;
        
    }
    if ([self.textFiledArray[1] text].length>20) {
        showTipHudStr(@"新密码不多于15位数");
        return;
    }

     if (![SSSRegularExpression checkPassword:[self.textFiledArray[1] text]]) {
         showTipHudStr(@"新密码只包含字母和数字");
         return;

    }
    if (![[self.textFiledArray[1] text] isEqualToString:[self.textFiledArray[2] text]]){
        showTipHudStr(@"两次输入密码不一致");
        return;
    }
    
    [[XHRequestManager manager] postRequest:kConfigPath(kReviseTradeCode) params:@{@"oldTransPwd":[self.textFiledArray[0] text],@"transactionPwd":[self.textFiledArray[1] text]} success:^(id  _Nonnull responseObj) {
        if (kResponseStatus==0) {
            [QMUITips showSucceed:@"修改交易密码成功!" inView:kKeyWindow hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }
        else showTipHudStr(kResponseMessage);

        
    } failure:^(NSError * _Nonnull error) {
        
        
    }];
    
    [ZDNetManager zd_requestWithType:ZDHttpRequestTypePost UrlString:kConfigPath(kReviseTradeCode) Parameters:@{@"oldTransPwd":[self.textFiledArray[0] text],@"transactionPwd":[self.textFiledArray[1] text]} SuccessBlock:^(id responseObj) {
        if (kResponseStatus==0) {
            [QMUITips showSucceed:@"修改交易密码成功!" inView:kKeyWindow hideAfterDelay:1];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }
        else showTipHudStr(kResponseMessage);
        
    } FailureBlock:^(NSError *error) {
        
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
    } progress:nil];
    

}
/**
 *  点击空白处，让键盘消失
 *
 *  @param touches 手指触摸的地方
 *  @param event   手指触摸
 */
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 让所有的文本框放弃第一响应者
    [self doResignFirstResponder];
}
#pragma mark - 取消按钮
- (void)cancelBtnClick{
    
    
}
/**
 *  让所有的文本框放弃第一响应者的回调方法
 */
- (void)doResignFirstResponder {
    [self.textFiledArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj resignFirstResponder];
    }];
}

/**
 *  懒加载，初始化数组
 *
 *  @return 返回一个初始化好的数组
 */
- (NSMutableArray *)textFiledArray {
    if (!_textFiledArray) {
        _textFiledArray = [NSMutableArray array];
    }
    return _textFiledArray;
}

@end
